//
//  BaseDrawerViewController.swift
//  QQ
//
//  Created by hjl on 2017/6/8.
//  Copyright © 2017年 hjl. All rights reserved.
//

import UIKit

let kScreenW = UIScreen.main.bounds.width
let kScreenH = UIScreen.main.bounds.height

let kMenuViewW = kScreenW * 0.8
let kMenuViewH = kScreenH

class BaseDrawerViewController: UIViewController {
    
    // 菜单
    var menuView : UIView!
    // 主视图
    var mainView : UIView!
    // 主菜单蒙版
    var maskingView : UIView!
    // 敲击手势
    var tap : UITapGestureRecognizer!
    // 拖拽手势
    var pan : UIPanGestureRecognizer! = nil
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupChildView()
        setupGestureRecognizer()
    }

}

extension BaseDrawerViewController: UIGestureRecognizerDelegate {
    
    func setupChildView() {
        menuView = UIView.init(frame: CGRect.init(x: -kScreenW * 0.5, y: 0, width: kMenuViewW, height: kMenuViewH))
        view.addSubview(menuView)
        
        mainView = UIView.init(frame: view.bounds)
        view.addSubview(mainView)
        
        maskingView = UIView.init(frame: view.bounds)
        maskingView.backgroundColor = UIColor.init(red: 1, green: 1, blue: 1, alpha: 0.5)
    }
    
    
    func setupGestureRecognizer() {
        // 创建pan手势
        pan = UIPanGestureRecognizer.init(target: self, action: #selector(panClick(pan:)))
        // 添加pan手势
        mainView.addGestureRecognizer(pan)
        pan.delegate = self
    
        // 创建tap手势
        tap = UITapGestureRecognizer.init(target: self, action: #selector(tapClick(tap:)))
        tap.delegate = self
    }
    
    func menuViewShow() {
        // print("menuViewShow")
        UIView.animate(withDuration: 0.3) {
            self.menuView.frame.origin.x = 0
            self.mainView.frame.origin.x = kMenuViewW
            // 显示菜单的时候, 添加敲击手势
            self.mainView.addGestureRecognizer(self.tap)
        }
    }
    
    func menuViewHidden() {
        // print("menuViewHidden");
        UIView.animate(withDuration: 0.3) {
            self.menuView.frame.origin.x = -kScreenW * 0.5
            self.mainView.frame.origin.x = 0
            // 隐藏菜单的时候, 移除敲击手势
            self.mainView.removeGestureRecognizer(self.tap)
            // 移除蒙版
            self.maskingView.removeFromSuperview()
        }
    }   
}

extension BaseDrawerViewController {
    // 手势方法
    func panClick(pan: UIPanGestureRecognizer){
        // 获取手势移动的位置
        let translation : CGPoint = pan.translation(in: mainView)
        // 获取x轴上的偏移量
        let offsetX : CGFloat = translation.x
        // 禁止左滑
        if mainView.frame.origin.x <= 0 && offsetX < 0 {
            return
        }
        // 复原
        pan.setTranslation(CGPoint.zero, in: self.mainView)
        
        // mainView 进行偏移, 偏移距离为: offsetX
        menuView.frame = self.menuViewFrameWithOffsetX(offsetX: offsetX)
        mainView.frame = self.mainViewFrameWithOffsetX(offsetX: offsetX)
        
        // 当手势结束的时候,定位
        if pan.state == UIGestureRecognizerState.ended {
            if mainView.frame.origin.x > kScreenW * 0.4 {
                // 定位到右边
                self.menuViewShow()
            } else{
                self.menuViewHidden()
            }
        }
    }
    
    
    // 根据迁移量计算mainView.frame
    func mainViewFrameWithOffsetX(offsetX : CGFloat) -> CGRect {
        
        if self.maskingView.superview != self.mainView {
            self.mainView.insertSubview(self.maskingView, at: 999999)
        }
        
        // 获取上一次的frame
        var perframe : CGRect = mainView.frame
        // 跟新frame
        perframe.origin.x += offsetX
        if perframe.origin.x > kMenuViewW {
            perframe.origin.x = kMenuViewW
        }
        let kcolor = (kScreenW - perframe.origin.x) / kScreenW
        self.maskingView.backgroundColor = UIColor.init(red: kcolor, green: kcolor, blue: kcolor, alpha: 0.5)
        
        return perframe
    }
    
    func menuViewFrameWithOffsetX(offsetX : CGFloat) -> CGRect {
        // 获取上一次的frame
        var perframe : CGRect = menuView.frame
        // 跟新frame
        perframe.origin.x += offsetX * 0.5 / 0.8
        if perframe.origin.x > 0 {
            perframe.origin.x = 0
        }
        return perframe
    }
    
    // 敲击时隐藏菜单
    func tapClick(tap : UITapGestureRecognizer) {
        self.menuViewHidden()
    }
    
    // 判断是不是敲击的 mainview的 左侧1/5
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
        // 不用进行判断, 是哪一个手势, 两个手势刚好都只设成左侧1/5区域有效
        let point = touch.location(in: self.mainView)
        if point.x < kScreenW * 0.2 {
            return true
        }
        return false
    }
}

